#include <allegro5\allegro_audio.h>
#include "sw_sound_manager.h"
#include "windows.h"
#include <string>


sw_sound_manager::sw_sound_manager() {
	soundfiles = new std::string[sw_sound_types_num];
	instances = new ALLEGRO_SAMPLE_INSTANCE*[sw_sound_types_num];
	playing = new bool[sw_sound_types_num];
	for (int i=0; i<sw_sound_types_num; i++) {
		playing[i] = false;
	}
	streams = new ALLEGRO_AUDIO_STREAM*[sw_sound_types_num];
}

sw_sound_manager::~sw_sound_manager() {
	int numsoundtypes = 3;
	for (int i=0; i<numsoundtypes; i++) {
		if (instances[i]!=NULL) al_destroy_sample_instance(instances[i]);
	}
	delete[] instances;
	delete[] playing;
	delete[] streams;
}

void sw_sound_manager::set_sound(int sound_type, char* filename) {
	std::string s(filename);
	s = "sounds/" + s;
	soundfiles[sound_type] = s;
	//instances[sound_type] = al_create_sample_instance(al_load_sample(s.c_str()));
	//al_attach_sample_instance_to_mixer(instances[sound_type], al_get_default_mixer());
	//streams[sound_type] = al_load_audio_stream(s.c_str(), 80, 8);
	//al_attach_audio_stream_to_mixer(streams[sound_type], al_get_default_mixer());
}

void sw_sound_manager::play_sound(int sound_type, bool restart) {
	//if (restart) al_stop_sample_instance(instances[sound_type]);
	//al_play_sample_instance(instances[sound_type]);
	//if (!playing[sound_type]) {
	//	PlaySound("song.wav", NULL, SND_FILENAME | SND_ASYNC);
	//	playing[sound_type] = true;
	//}
	//if (restart && streams[sound_type]!=NULL) {
	//	stop_audio_stream(
	//}
	if (!playing[sound_type]) {
		streams[sound_type] = al_load_audio_stream(soundfiles[sound_type].c_str(), 80, 8);
		al_attach_audio_stream_to_mixer(streams[sound_type], al_get_default_mixer());
		playing[sound_type] = true;
	}
}